<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
    <!--    <el-col :span="12">
            <el-form-item label="广告位" prop="advGroup.id"
                :rules="[
                  {required: true, message:'广告位不能为空', trigger:'blur'}
                 ]">
          <SelectTree
                      ref="advGroup"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"

                      url="/smis/kioskadv/jfKioskAdvGroup/treeData"
                      :value="inputForm.advGroup.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.advGroup.id=value}"/>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="广告名" prop="name"
                :rules="[
                  {required: true, message:'广告名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写广告名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="轮播时间(秒)" prop="shufTime"
                :rules="[
                  {required: true, message:'轮播时间(秒)不能为空', trigger:'blur'},
                  {validator: validator.isIntGtZero, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.shufTime" placeholder="请填写轮播时间(秒)"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="播放设备" prop="kioskIdList"
               :rules="[
                 {required: true, message:'播放设备不能为空', trigger:'blur'}
                ]">
             <div class="block">
               <el-cascader
                 ref="kioskSelect"
                 collapse-tags
                 :options="options"
                 :props="{ multiple: true ,emitPath: false}"
                 v-model="inputForm.kioskIdList"
                 @change="changeCascade"
                 clearable></el-cascader>

             </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="排序" prop="sort"
                :rules="[
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="是否上架" prop="state"
                :rules="[
                  {required: true, message:'是否上架不能为空', trigger:'blur'}
                 ]">
                    <el-radio-group v-model="inputForm.state">
                        <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
                    </el-radio-group>
           </el-form-item>
        </el-col>
        <el-col :span="22" :offset="2" style="color: #da5353;font-size: 13px;line-height: 2;" v-show="inputForm.state === '1'">
          {{getNotice()}}
        </el-col>
        <el-col :span="12" v-show="inputForm.state=== '1'">
            <el-form-item label="上架时间" prop="upDate"
                :rules="[
                 ]">
                <el-date-picker
                      v-model="inputForm.upDate"
                      type="date"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd"
                      placeholder="选择上架时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-show="inputForm.state=== '1'">
            <el-form-item label="下架时间" prop="downDate"
                :rules="[
                 ]">
                <el-date-picker
                  v-model="inputForm.downDate"
                  type="date"
                  style="width: 100%;"
                  value-format="yyyy-MM-dd"
                  placeholder="选择下架时间">
                </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="广告收益" prop="advFee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.advFee" placeholder="广告收益非必填"     ></el-input>
           </el-form-item>
        </el-col>

        <!-- <el-col :span="12">
            <el-form-item label="跳转类型" prop="openType"
                :rules="[
                 ]">
                <el-select v-model="inputForm.openType" placeholder="请选择"  style="width: 100%;">
                    <el-option
                      v-for="item in $dictUtils.getDictList('kiosk_open_type')"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                </el-select>
           </el-form-item>
        </el-col> -->
        <el-col :span="12" v-if="inputForm.openType === 'jump_merchant'">
              <el-form-item label="商户" prop="openUrl"
                  :rules="[
                   ]">
              <GridSelect
                title="选择商户"
                labelName = 'name'
                labelValue = 'id'
                :value = "inputForm.openUrl"
                :limit="1"
                @getValue='(value) => {inputForm.openUrl=value}'
                :columns="[
                {
                  prop: 'name',
                  label: '商户名称'
                }
                ]"
                :searchs="[
                {
                  prop: 'name',
                  label: '商户名称'
                }
                ]"
                dataListUrl="/smis/merchants/jfMerchants/list"
                entityBeanName="jfMerchants"
                queryEntityUrl="/smis/merchants/jfMerchants/queryById">
              </GridSelect>
             </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图片/视频" prop="files"
               :rules="[
                ]">
             <el-upload-ext
               :limit = "30"
               :accept = "inputForm.advGroup.suffix"
               fileType = "2"
               :allowOtherSize = "false"
               :width = "inputForm.advGroup.width"
               :height = "inputForm.advGroup.height"
               :allowScale = "false"
               v-model = "inputForm.files"
             >
             </el-upload-ext>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import ElUploadExt from '@/components/elUploadExt'
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        filesArra: [],
        inputForm: {
          id: '',
          advGroup: {
            id: '',
            "width":"300",
            "height":"400",
            "suffix":".png"
          },
          name: '',
          sort: '',
          shufTime: '5',
          state: '',
          upDate: '',
          downDate: '',
          advFee: '',
          kioskIds: '',
          kioskIdList: [],
          kioskNames: '',
          files: '',
          openType: 'no_jump',
          openUrl: ''
        },
        options: []
      }
    },
    components: {
      ElUploadExt,
      GridSelect
    },
    methods: {
      init (method, id, searchForm) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建广告`
        } else if (method === 'edit') {
          this.title = '修改广告'
        } else if (method === 'view') {
          this.title = '查看广告'
        }
        // this.filesArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/kioskadv/jfKioskAdvList/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfKioskAdvList)
              this.inputForm.kioskIdList = (this.inputForm.kioskIds || '').split(",")
              this.loading = false
              console.log(this.inputForm)
            })
          } else {
            this.inputForm.state = '1'
            this.inputForm.shufTime = '15'
            this.inputForm.advGroup =  searchForm.advGroup
          }
          this.$http({
            url: `/smis/kiosk/jfKiosk/kioskJson`,
            method: 'get'
          }).then(({data}) => {
            this.options = data
            this.loading = false
          })
        })
      },
      // 切换选项
      changeCascade (val) {
        console.info(val)
      },
      getNotice() {
        var startDate = ''
        var endDate = ''
        if(this.inputForm.upDate) {
          if(this.inputForm.upDate.length === 10) {
            startDate = this.inputForm.upDate+" 00:00:00"
          } else {
            startDate = this.inputForm.upDate
          }
        }
        if(this.inputForm.downDate) {
          if(this.inputForm.downDate.length === 10) {
            endDate = this.inputForm.downDate+" 23:59:59"
          } else  {
            endDate = this.inputForm.downDate.replace(' 00:00:00', ' 23:59:59')
          }
        }
        if(startDate !== '' && endDate !== '') {
          return '当前广告设定的期限为：'+startDate+' ～ '+endDate
        } else if(startDate !== '') {
          return '当前广告设定的期限为：'+startDate+'起'
        } else if(endDate !== '') {
          return '当前广告设定的期限为：'+endDate+'止'
        }
        return '当前广告设定的期限为：长期有效'
      },
      // 表单提交
      doSubmit () {
        var nodes = this.$refs.kioskSelect.getCheckedNodes(true)
        this.inputForm.kioskIds = this.inputForm.kioskIdList.join(",")
        this.inputForm.kioskNames = nodes.map(node => node.label||'').join(',')
        if(this.inputForm.upDate && this.inputForm.upDate.length === 10) {
          this.inputForm.upDate = this.inputForm.upDate+" 00:00:00"
        }
        if(this.inputForm.downDate && this.inputForm.downDate.length === 10) {
          this.inputForm.downDate = this.inputForm.downDate+" 23:59:59"
        }
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/kioskadv/jfKioskAdvList/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
